exports.comment = (req,res)=> { 
    
    /*
      params:{
	     comment:评论的内容
	     author：评论的作者，
	     id: 评论的文章
      }  
  
    */
    let comment = global.dbHelper.getModel('comment');  
    if(req.method == 'POST'){
		    let commentText = req.body.commentText,
		        author = req.body.author,    
		        id = req.body.id ;   


		    let time = new Date().getFullYear()+'-'+(new Date().getMonth()+1)+'-'+new Date().getDate()+' '+new Date().getHours()+':'+new Date().getMinutes()+':'+new Date().getSeconds();
		   
		    comment.create({
		       commentText : commentText,
		       author : author,
		       id : id,
		       time : time
		    },(error,doc) =>{
		        if(error){
			 	 	logger.error("评论创建失败:"+error)
			 	 	res.json({
			 	 		"code": 500,
			 	 		"msg":"评论失败"
			 	 	})
			 	 }else{
			 	 	res.json({
			 	 		"code": 200,
			 	 		"msg": "评论成功"
			 	 	})
			 	 }   

		    })
     }else{
	      let id = req.query.id;
          let rows = Math.floor(req.query.rows) || 10 ;   //每页显示条数
          let index = Math.floor(req.query.index) || 1;   //当前页数

          comment.count({ 
          	   id : id
           },(err,doc)=>{
	     	   pagesize = doc;
	      })

	      comment.find({
		       id : id,
		   },(error,doc) =>{
		        if(error){
			 	 	logger.error("查找评论失败:"+error)
			 	 	 var data = {
	                    code:500,
	                    msg:"查询失败"
	                  }
			 	 }else{
			 	 	 let n = Math.ceil(pagesize / rows);  //总共多少页
			 	 	 var data = {
		                code:200,
		                msg:"查询成功",
		                totalItem:pagesize, //总条数
		                totalPage:n, //总共多少页
		                rows:rows,//每页显示多少条
		                index:index,//当前页数
		                result:doc
		            }
			 	 }  

			 	 res.json({
		            result:data
		         }) 
 
		}).sort({"time": -1}).skip((index - 1) * rows).limit(rows);
          
     }
  }
